﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Xml;
using System.Net.Sockets;

namespace GameAiLaTrieuPhu_Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Player player;
        private bool isConneted;
        private const string SERVER_ADDR = "127.0.0.1";

        public MainWindow()
        {
            InitializeComponent();
            isConneted = false;
            HideAllRegisterBar();
            HideAnswerBar();
            HideQuestionBar();
            HideSystemBar();
        }

        // Process Event -------------------------------------------------------------------------------------------------
        // Connect to Server
        private void ConnectServer()
        {
            // TO DO: 
            // Try { }
            RenameRegisterBar();
            isConneted = true;
            // Catch {}
            tblRegisterNotice.Text = "Kết nối không thành công!";
        }

        // Check Nickname: 'a'...'z', 'A'...'Z', '0'...'9'
        private bool IsPossibleNickname(string strNickname)
        {
            for (int i = 0; i < strNickname.Length; i++)
            {
                if (strNickname[i] < 48 || strNickname[i] > 122)
                {
                    return false;
                }
                else
                {
                    if ((strNickname[i] > 57 && strNickname[i] < 65) || (strNickname[i] > 90 && strNickname[i] < 97))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
       
        private void RegisterNickname()
        {
            // Show Bar
            ShowAllRegisterBar();
            ShowSystemBar();

            string strNickname = tbxNickname.Text;

            // TO DO: Check Nickname
            //{
            bool isPossibleNickname = true; // Unit test, defined true  
            //}
            if (isPossibleNickname)
            {
                player = new Player(strNickname);
            }
            else
            {
                tblSystemMessage.Text = "Nickname của bạn đã được đăng ký.\nVui lòng chọn lại một Nickname khác!";
            }
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            if (isConneted)
            {
                RegisterNickname();
            }
            else
            {
                ConnectServer();
            }
        }

        // Process Hide and Show Controls --------------------------------------------------------------------------------
        private void RenameRegisterBar()
        {
            tblDangKyTroChoi.Text = "Đăng ký Tài khoản";            
            btnRegister.Content = "Đăng ký";
            tblRegisterNotice.Text = "Nickname được tạo thành từ các ký tự từ 'a'...'z', 'A'...'Z', '0'...'9'";
        }

        private void HideAllRegisterBar()
        {
            tblDangKyTroChoi.Visibility = Visibility.Hidden;
            tblNickname.Visibility = Visibility.Hidden;
            tbxNickname.Visibility = Visibility.Hidden;
            tblRegisterNotice.Visibility = Visibility.Hidden;
            btnRegister.Visibility = Visibility.Hidden;
        }

        private void HideRegisterBar()
        {
            tblNickname.Visibility = Visibility.Hidden;
            tbxNickname.Visibility = Visibility.Hidden;
            btnRegister.Visibility = Visibility.Hidden;
        }

        private void HideSystemBar()
        {
            tblThongBaoTuHeThong.Visibility = Visibility.Hidden;
            tblSystemMessage.Visibility = Visibility.Hidden;
        }

        private void HideAnswerBar()
        {
            tblTraLoiCauHoi.Visibility = Visibility.Hidden;
            tbxAnswerContent.Visibility = Visibility.Hidden;
            btnSendAnswer.Visibility = Visibility.Hidden;
            btnSkipQuestion.Visibility = Visibility.Hidden;
        }

        private void HideQuestionBar()
        {
            tblCauHoiChuongTrinh.Visibility = Visibility.Hidden;
            tblTheLoai.Visibility = Visibility.Hidden;
            tblQuestionCategory.Visibility = Visibility.Hidden;
            tblNoiDung.Visibility = Visibility.Hidden;
            tblQuestionContent.Visibility = Visibility.Hidden;
        }

        private void ShowAllRegisterBar()
        {
            tblDangKyTroChoi.Visibility = Visibility;
            tblNickname.Visibility = Visibility;
            tbxNickname.Visibility = Visibility;
            tblRegisterNotice.Visibility = Visibility;
            btnRegister.Visibility = Visibility;
        }

        private void ShowRegisterBar()
        {
            tblNickname.Visibility = Visibility;
            tbxNickname.Visibility = Visibility;
            btnRegister.Visibility = Visibility;
        }

        private void ShowSystemBar()
        {
            tblThongBaoTuHeThong.Visibility = Visibility;
            tblSystemMessage.Visibility = Visibility;
        }

        private void ShowAnswerBar()
        {
            tblTraLoiCauHoi.Visibility = Visibility;
            tbxAnswerContent.Visibility = Visibility;
            btnSendAnswer.Visibility = Visibility;
            btnSkipQuestion.Visibility = Visibility;
        }

        private void ShowQuestionBar()
        {
            tblCauHoiChuongTrinh.Visibility = Visibility;
            tblTheLoai.Visibility = Visibility;
            tblQuestionCategory.Visibility = Visibility;
            tblNoiDung.Visibility = Visibility;
            tblQuestionContent.Visibility = Visibility;
        }

        // Process Mouse -------------------------------------------------------------------------------------------------
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        public const int MAX_PLAYER = 10;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg,
                int wParam, int lParam);

        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void MoveWindow(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            ReleaseCapture();
            SendMessage(new WindowInteropHelper(this).Handle,
                WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }

        private void EXIT(object sender, MouseButtonEventArgs e)
        {
            Environment.Exit(0);
        }

        private void MINIMIZE(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void ActivateTitleIcons(object sender, MouseEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose02"];
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize02"];
        }

        private void DeactivateTitleIcons(object sender, MouseEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose01"];
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize01"];
        }

        private void PressingButtonClose(object sender, MouseButtonEventArgs e)
        {
            btnClose.Fill = (ImageBrush)Main.Resources["ButtonClose03"];
        }

        private void PressingButtonMinimize(object sender, MouseButtonEventArgs e)
        {
            btnMinimize.Fill = (ImageBrush)Main.Resources["ButtonMinimize03"];
        }        
    }
}
